<?php
/**
 * ===============================================
 * 版权所有 2019-2050 青岛铁锤科技有限公司，并保留所有权利。
 * 网站地址: http://www.huaxianling.com
 * ===============================================
 * Author: Rag Hu
 * Created Time: 2019/8/21 9:37
 * Modified Time: 2019/8/21 9:37
 * Version: 2.0
 */

namespace App\Modules\Order\Criterias;


use Flower\Core\Repository\Contracts\RepositoryInterface;
use Flower\Core\Repository\Criterias\BaseCriterias;

class GetOrderPointCriteria extends BaseCriterias
{
    protected $start_time;
    protected $end_time;
    protected $store_id;
    protected $user_id;

    public function __construct($start_time, $end_time, $store_id, $user_id = null)
    {
        $this->start_time = $start_time;
        $this->end_time = $end_time;
        $this->store_id = $store_id;
        $this->user_id = $user_id;
    }

    public function apply($model, RepositoryInterface $repository)
    {
        $date = date('Y-m-d H:i:s', time());
        $start_time = $this->start_time;
        $end_time = $this->end_time;
        $store_id = $this->store_id;
        $user_id = $this->user_id;
        return $model
            ->select([
                'order.id',
                'member_id',
                'order.created_at'
            ])
            ->with(['member'=>function($query){
                $query->select(['id','name','face']);
            }])
            ->where('store_id', $store_id)
            ->where('is_appoint', 1)
            ->whereRaw('TO_DAYS( "' . $date . '" ) - TO_DAYS(hx_order.created_at) >= ' . $start_time)
            ->whereRaw('TO_DAYS( "' . $date . '" ) - TO_DAYS(hx_order.created_at) < ' . $end_time)
            ->when($user_id, function ($query) use ($user_id) {
                $query->where('barber_id', $user_id);
            })
            ->orderBy('order.id','DESC');
    }
}